package com.nstar.orderexpress.model
{
	import com.adobe.cairngorm.model.ModelLocator;
	import com.nstar.orderexpress.view.OrderExpressApp;
	import com.nstar.orderexpress.vo.UserInfoVO;

	[Bindable]
	public class Model implements ModelLocator
	{
		private static var model:Model;
		
		private var mModels:Object;
		public var mainApp:OrderExpressApp;//应用的引用
		public var isDebuging:Boolean = false;
		public var userInfo:UserInfoVO = new UserInfoVO();
		
		//单例模式
		public static function getInstance():Model 
		{
			if ( model == null )
				model = new Model();
				
			return model;
	   }		
	   
	   	// Constructor should be private but current AS3.0 does not allow it yet (?)...
	   	public function Model() 
	   	{	
	   		mModels = new Object();
	   		if ( Model.model != null )
				throw new Error( "Only one ModelLocator instance should be instantiated" );	
	   	}	 
	   	
		/**
		* Adds a model to the list of models.
		* 
		* @param	modelName	Name of the model, for named look-up.
		* @param	modelRef	Reference to the actual data object.
		*/
		public function addModel( modelName:String, modelRef:* ):void
		{
			trace ( "ModelLocator.addModel(). Adding " + modelName + "." );
			// Log.info ( modelRef );
			mModels [ modelName ] = modelRef;
		}
		
		/**
		* 	Returns reference to model with given name. Untyped on purpose.
		* 
		* @param	modelName	The name of the model object being requested.
		* @return	Reference to the requested data object or null if it
		* 			cannot be found.
		*/
		public function getModel ( modelName:String ):*
		{
			var model:* = mModels [ modelName ];
			
			if ( undefined == model )
			{
				trace ( "ModelLocator.getModel(). Could not find model " + modelName );
				return null;
			}
			else
			{
				return model;
			}
		}	   	  
	}
}